package com.gpframework.module.controller.ctn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.module.ctn.domain.*;
import com.gpframework.module.ctn.domain.req.*;
import com.gpframework.module.ctn.domain.vo.GpCtnLoadedSummaryVo;
import com.gpframework.module.ctn.domain.vo.GpFormVo;
import com.gpframework.module.ctn.domain.vo.GpPackagesInformationVo;
import com.gpframework.module.ctn.domain.vo.GpSupplierSummaryVo;
import com.gpframework.module.ctn.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZS
 * @version 1.0
 * @date 2024/1/15 16:05
 */
@Validated
@Api(tags = "报表统计接口")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/form")
public class GpFormsStatistics extends BaseController {

    private final IGpFormsStatisticsService iGpFormsStatisticsService;
    private final IGpReceiptGoodsService iGpReceiptGoodsService;
    private final IGpPackagesInformationService iGpPackagesInformationService;
    private final IGpSackInformationService iGpSackInformationService;
    private final IGpCtnOrderService iGpCtnOrderService;


    @ApiOperation("供应商汇总表")
    @PreAuthorize("@ss.hasPermi('supplierSummary')")
    @GetMapping("/supplierSummary")
    public TableDataInfo<GpSupplierSummaryVo> supplierSummary(GpSupplierSummaryReq gpSupplierSummaryReq) {
        startPage();
        QueryWrapper<GpReceiptGoods> lqw = new QueryWrapper<GpReceiptGoods>();
        gpSupplierSummaryReq.generatorQuery(lqw, true);
        List<GpSupplierSummaryVo> list = iGpFormsStatisticsService.supplierSummary(lqw);
        return getDataTable(list);
    }


    @ApiOperation("客户汇总表")
    @PreAuthorize("@ss.hasPermi('customerSummary')")
    @GetMapping("/customerSummary")
    public TableDataInfo<GpSupplierSummaryVo> customerSummary(GpCustomerSummaryReq gpCustomerSummaryReq) {
        startPage();
        QueryWrapper<GpReceiptGoods> lqw = new QueryWrapper<GpReceiptGoods>();
        gpCustomerSummaryReq.generatorQuery(lqw, true);
        List<GpSupplierSummaryVo> list = iGpFormsStatisticsService.customerSummary(lqw);
        return getDataTable(list);
    }

    @ApiOperation("清关公司单个柜装柜清单")
    @PreAuthorize("@ss.hasPermi('loadingListAgentOne')")
    @GetMapping("/loadingListAgentOne")
    public TableDataInfo<GpCtnLoadedSummaryVo> loadingListAgentOne(GpLoadingListAgentOneReq gpLoadingListAgentOneReq) {
        startPage();
        QueryWrapper<GpCtnLoaded> lqw = new QueryWrapper<>();
        gpLoadingListAgentOneReq.generatorQuery(lqw, true);
        List<GpCtnLoadedSummaryVo> list = iGpFormsStatisticsService.loadingListAgentOne(lqw);
        return getDataTable(list);
    }

    @ApiOperation("供应商、客户清关公司查询 ")
    @PreAuthorize("@ss.hasPermi('supperCustomerAgent')")
    @GetMapping("/supperCustomerAgent")
    public TableDataInfo<GpCtnLoadedSummaryVo> supperCustomerAgent(GpSupplierCustomerAgentReq gpSupplierCustomerAgentReq) {
        startPage();
        QueryWrapper<GpCtnLoaded> lqw = new QueryWrapper<>();
        gpSupplierCustomerAgentReq.generatorQuery(lqw, true);
        List<GpCtnLoadedSummaryVo> list = iGpFormsStatisticsService.supplierCustomerAgent(lqw);
        return getDataTable(list);
    }


    @ApiOperation("货柜(年度)统计表")
    @PreAuthorize("@ss.hasPermi('containerAnnualStatistics')")
    @GetMapping("/containerAnnualStatistics")
    public TableDataInfo<GpCtnLoadedSummaryVo> containerAnnualStatistics(GpContainerAnnualStatisticsReq gpContainerAnnualStatisticsReq) {
        startPage();
        QueryWrapper<GpCtnReachPort> lqw = new QueryWrapper<>();
        gpContainerAnnualStatisticsReq.generatorQuery(lqw, true);
        List<GpCtnLoadedSummaryVo> list = iGpFormsStatisticsService.gpContainerAnnualStatisticsReq(lqw);
        return getDataTable(list);
    }

    @ApiOperation("清关公司所有货柜货物统计表")
    @PreAuthorize("@ss.hasPermi('agentContainerStatistics')")
    @GetMapping("/agentContainerStatistics")
    public TableDataInfo<GpCtnLoadedSummaryVo> agentContainerStatistics(GpAgentContainerStatisticsReq gpAgentContainerStatisticsReq) {
        startPage();
        QueryWrapper<GpCtnLoaded> lqw = new QueryWrapper<>();
        gpAgentContainerStatisticsReq.generatorQuery(lqw, true);

        List<GpCtnLoadedSummaryVo> list = iGpFormsStatisticsService.agentContainerStatistics(lqw);
        return getDataTable(list);
    }

    //@ApiOperation("仓库未装柜货物统计表")
    //@PreAuthorize("@ss.hasPermi('warehouseNoLoadingSummary')")
    //@GetMapping("/warehouseNoLoadingSummary")
    //public TableDataInfo<GpCtnLoadedSummaryVo> warehouseNoLoadingSummary(GpPackagesInformationFromReq gpPackagesInformationReq) {
    //    startPage();
    //    QueryWrapper<GpPackagesInformation> lqw = new QueryWrapper<>();
    //    //gpPackagesInformationReq.generatorQuery(lqw, true);
    //    lqw.isNull("gp.ctn_order_id");
    //
    //    lqw.isNull("gpsack.ctn_order_id");
    //
    //    List<GpCtnLoadedSummaryVo> list = iGpFormsStatisticsService.warehouseNoLoadingSummary(lqw);
    //    return getDataTable(list);
    //}


    @ApiOperation("查询待处理数量")
    @PreAuthorize("@ss.hasPermi('getTodoCount')")
    @GetMapping("/getTodoCount")
    public AjaxResult getTodoCount() {
        Map<String, Object> j = new HashMap<>();
        //订单待处理 （待定状态）
        QueryWrapper<GpReceiptGoods> gpReceiptGoodsQueryWrapper = new QueryWrapper<>();
        gpReceiptGoodsQueryWrapper.eq("compress_or_sack", "2");
        int orderTodocount = iGpReceiptGoodsService.count(gpReceiptGoodsQueryWrapper);
        j.put("OrderTodoCount", orderTodocount);

        //国内扣货数量(放货数量)
        QueryWrapper<GpPackagesInformation> packagesQueryWrapper = new QueryWrapper<>();
        QueryWrapper<GpSackInformation> sackQueryWrapper = new QueryWrapper<>();
        packagesQueryWrapper.eq("hold_or_not", "0");
        sackQueryWrapper.eq("hold_or_not", "0");
        int packReleaseGoodsCount = iGpPackagesInformationService.count(packagesQueryWrapper);
        int sackReleaseGoodsCount = iGpSackInformationService.count(sackQueryWrapper);
        j.put("releaseGoodsCount", packReleaseGoodsCount+sackReleaseGoodsCount);

        //国内扣货数量(扣货数量)
        QueryWrapper<GpPackagesInformation> gpPackagesInformationQueryWrapper = new QueryWrapper<>();
        QueryWrapper<GpSackInformation> sackInformationQueryWrapper = new QueryWrapper<>();
        gpPackagesInformationQueryWrapper.eq("hold_or_not", "1");
        sackInformationQueryWrapper.eq("hold_or_not", "1");
        int packHoldGoodsCount = iGpPackagesInformationService.count(gpPackagesInformationQueryWrapper);
        int sackHoldGoodsCount = iGpSackInformationService.count(sackInformationQueryWrapper);
        j.put("holdGoodsCount", packHoldGoodsCount+sackHoldGoodsCount);

        return AjaxResult.success(j);
    }



    @ApiOperation("未装柜统计表")
    @PreAuthorize("@ss.hasPermi('noLoadingSummary')")
    @GetMapping("/noLoadingSummary")
    public TableDataInfo<GpCtnLoadedSummaryVo> noLoadingSummary(GpPackagesInformationReq gpPackagesInformationReq) {
        startPage();
        List<GpPackagesInformationVo> list= iGpFormsStatisticsService.noLoadingSummary(gpPackagesInformationReq.getReceiptNo(),gpPackagesInformationReq.getSupplier(),gpPackagesInformationReq.getSupplierTel(),gpPackagesInformationReq.getReleaseCustomer(),gpPackagesInformationReq.getReleaseCustomerTel());
        return getDataTable(list);
    }

    @ApiOperation("扣货未放货统计表")
    @PreAuthorize("@ss.hasPermi('holdAndNoReleased')")
    @GetMapping("/holdAndNoReleased")
    public TableDataInfo<GpCtnLoadedSummaryVo> holdAndNoReleased(GpPackagesInformationReq gpPackagesInformationReq) {
        startPage();
        List<GpPackagesInformationVo> gpPackagesInformationVos= iGpFormsStatisticsService.holdAndNoReleased(gpPackagesInformationReq.getReceiptNo(),gpPackagesInformationReq.getSupplier(),gpPackagesInformationReq.getSupplierTel(),gpPackagesInformationReq.getReleaseCustomer(),gpPackagesInformationReq.getReleaseCustomerTel());
        return getDataTable(gpPackagesInformationVos);
    }



    @ApiOperation("扣货已放货统计表")
    @PreAuthorize("@ss.hasPermi('holdAndReleased')")
    @GetMapping("/holdAndReleased")
    public TableDataInfo<GpPackagesInformationVo> holdAndReleased(GpPackagesInformationReq gpPackagesInformationReq) {
        startPage();
        List<GpPackagesInformationVo> gpPackagesInformationVos= iGpFormsStatisticsService.holdAndReleased(gpPackagesInformationReq.getReceiptNo(),gpPackagesInformationReq.getSupplier(),gpPackagesInformationReq.getSupplierTel(),gpPackagesInformationReq.getReleaseCustomer(),gpPackagesInformationReq.getReleaseCustomerTel());
        return getDataTable(gpPackagesInformationVos);
    }



    //@ApiOperation("扣货已放货统计表")
    //@PreAuthorize("@ss.hasPermi('holdAndReleased')")
    //@GetMapping("/holdAndReleased")
    //public TableDataInfo<GpCtnLoadedSummaryVo> holdAndReleased(GpPackagesInformationReq gpPackagesInformationReq) {
    //    startPage();
    //    //国内扣货数量(放货数量)
    //    QueryWrapper<GpPackagesInformation> packagesQueryWrapper = new QueryWrapper<>();
    //    packagesQueryWrapper.eq("hold_or_not", "0");
    //    packagesQueryWrapper.eq("del_status", "0");
    //    if (gpPackagesInformationReq.getReceiptNo()!=null){
    //        packagesQueryWrapper.eq("receipt_no", gpPackagesInformationReq.getReceiptNo());
    //    }
    //    if (gpPackagesInformationReq.getSupplier()!=null){
    //        packagesQueryWrapper.eq("supplier", gpPackagesInformationReq.getSupplier());
    //    }
    //    if (gpPackagesInformationReq.getSupplierTel()!=null){
    //        packagesQueryWrapper.eq("supplier_tel", gpPackagesInformationReq.getSupplierTel());
    //    }
    //    if (gpPackagesInformationReq.getReleaseCustomer()!=null){
    //        packagesQueryWrapper.eq("release_customer", gpPackagesInformationReq.getReleaseCustomer());
    //    }
    //    if (gpPackagesInformationReq.getReleaseCustomerTel()!=null){
    //        packagesQueryWrapper.eq("release_customer_tel", gpPackagesInformationReq.getReleaseCustomerTel());
    //    }
    //    List<GpPackagesInformationVo> gpPackagesInformationVos = iGpPackagesInformationService.listVo(packagesQueryWrapper);
    //
    //    QueryWrapper<GpSackInformation> sackQueryWrapper = new QueryWrapper<>();
    //    sackQueryWrapper.eq("hold_or_not", "0");
    //    sackQueryWrapper.eq("del_status", "0");
    //    if (gpPackagesInformationReq.getReceiptNo()!=null){
    //        sackQueryWrapper.eq("receipt_no", gpPackagesInformationReq.getReceiptNo());
    //    }
    //    if (gpPackagesInformationReq.getSupplier()!=null){
    //        sackQueryWrapper.eq("supplier", gpPackagesInformationReq.getSupplier());
    //    }
    //    if (gpPackagesInformationReq.getSupplierTel()!=null){
    //        sackQueryWrapper.eq("supplier_tel", gpPackagesInformationReq.getSupplierTel());
    //    }
    //    if (gpPackagesInformationReq.getReleaseCustomer()!=null){
    //        sackQueryWrapper.eq("release_customer", gpPackagesInformationReq.getReleaseCustomer());
    //    }
    //    if (gpPackagesInformationReq.getReleaseCustomerTel()!=null){
    //        sackQueryWrapper.eq("release_customer_tel", gpPackagesInformationReq.getReleaseCustomerTel());
    //    }
    //
    //
    //    List<GpSackInformationVo> gpSackInformationVoList = iGpSackInformationService.listVo(sackQueryWrapper);
    //
    //    for (GpSackInformationVo gpSackInformationVo : gpSackInformationVoList) {
    //        GpPackagesInformationVo gpSackagesInformation = Convert.convert(new TypeReference<GpPackagesInformationVo>() {
    //        }, gpSackInformationVo);
    //        gpPackagesInformationVos.add(gpSackagesInformation);
    //    }
    //
    //    return getDataTable(gpPackagesInformationVos);
    //}

    //
    //@ApiOperation("扣货未放货统计表")
    //@PreAuthorize("@ss.hasPermi('holdAndNoReleased')")
    //@GetMapping("/holdAndNoReleased")
    //public TableDataInfo<GpCtnLoadedSummaryVo> holdAndNoReleased(GpPackagesInformationReq gpPackagesInformationReq) {
    //
    //
    //    startPage();
    //    //国内扣货数量(放货数量)
    //    QueryWrapper<GpPackagesInformation> packagesQueryWrapper = new QueryWrapper<>();
    //    packagesQueryWrapper.eq("hold_or_not", "1");
    //    packagesQueryWrapper.eq("del_status", "0");
    //    if (gpPackagesInformationReq.getReceiptNo()!=null){
    //        packagesQueryWrapper.eq("receipt_no", gpPackagesInformationReq.getReceiptNo());
    //    }
    //    if (gpPackagesInformationReq.getSupplier()!=null){
    //        packagesQueryWrapper.eq("supplier", gpPackagesInformationReq.getSupplier());
    //    }
    //    if (gpPackagesInformationReq.getSupplierTel()!=null){
    //        packagesQueryWrapper.eq("supplier_tel", gpPackagesInformationReq.getSupplierTel());
    //    }
    //    if (gpPackagesInformationReq.getReleaseCustomer()!=null){
    //        packagesQueryWrapper.eq("release_customer", gpPackagesInformationReq.getReleaseCustomer());
    //    }
    //    if (gpPackagesInformationReq.getReleaseCustomerTel()!=null){
    //        packagesQueryWrapper.eq("release_customer_tel", gpPackagesInformationReq.getReleaseCustomerTel());
    //    }
    //    List<GpPackagesInformationVo> gpPackagesInformationVos = iGpPackagesInformationService.listVo(packagesQueryWrapper);
    //
    //    QueryWrapper<GpSackInformation> sackQueryWrapper = new QueryWrapper<>();
    //
    //    sackQueryWrapper.eq("hold_or_not", "1");
    //    sackQueryWrapper.eq("del_status", "0");
    //    if (gpPackagesInformationReq.getReceiptNo()!=null){
    //        sackQueryWrapper.eq("receipt_no", gpPackagesInformationReq.getReceiptNo());
    //    }
    //    if (gpPackagesInformationReq.getSupplier()!=null){
    //        sackQueryWrapper.eq("supplier", gpPackagesInformationReq.getSupplier());
    //    }
    //    if (gpPackagesInformationReq.getSupplierTel()!=null){
    //        sackQueryWrapper.eq("supplier_tel", gpPackagesInformationReq.getSupplierTel());
    //    }
    //    if (gpPackagesInformationReq.getReleaseCustomer()!=null){
    //        sackQueryWrapper.eq("release_customer", gpPackagesInformationReq.getReleaseCustomer());
    //    }
    //    if (gpPackagesInformationReq.getReleaseCustomerTel()!=null){
    //        sackQueryWrapper.eq("release_customer_tel", gpPackagesInformationReq.getReleaseCustomerTel());
    //    }
    //
    //
    //    List<GpSackInformationVo> gpSackInformationVoList = iGpSackInformationService.listVo(sackQueryWrapper);
    //
    //    for (GpSackInformationVo gpSackInformationVo : gpSackInformationVoList) {
    //        GpPackagesInformationVo gpSackagesInformation = Convert.convert(new TypeReference<GpPackagesInformationVo>() {
    //        }, gpSackInformationVo);
    //        gpPackagesInformationVos.add(gpSackagesInformation);
    //    }
    //
    //    return getDataTable(gpPackagesInformationVos);
    //}



    @ApiOperation("首页客户信息查询")
    @PreAuthorize("@ss.hasPermi('getCustomer')")
    @GetMapping("/getCustomer")
    public TableDataInfo<GpFormVo> getCustomer(GpReceiptGoodsReq gpReceiptGoodsReq) {
        startPage();
        QueryWrapper<GpReceiptGoods> lqw = new QueryWrapper<>();
        gpReceiptGoodsReq.generatorQuery(lqw, true);
        List<GpFormVo> list = iGpReceiptGoodsService.customerStatistics(lqw);
        return getDataTable(list);
    }


    @ApiOperation("首页供应商信息查询")
    @PreAuthorize("@ss.hasPermi('getCustomer')")
    @GetMapping("/getSupplier")
    public TableDataInfo<GpFormVo> getSupplier(GpReceiptGoodsReq gpReceiptGoodsReq) {
        startPage();
        QueryWrapper<GpReceiptGoods> lqw = new QueryWrapper<>();
        gpReceiptGoodsReq.generatorQuery(lqw, true);
        List<GpFormVo> list = iGpReceiptGoodsService.supplierStatistics(lqw);
        return getDataTable(list);
    }


}
